/**
 * 服务层统一入口
 */

// API客户端
export { 
  default as apiClient,
  intelligentUpdateApi, 
  statusApi, 
  configApi, 
  longTimeoutApi 
} from './apiClient.js'

// 业务服务
export { 
  default as imageUpdateService, 
  ImageUpdateService,
  STEP_TYPES,
  STEP_STATUS,
  ACTION_TYPES
} from './imageUpdateService.js'

// WebSocket服务
export { 
  default as webSocketService, 
  WebSocketService,
  setAutoConnectEnabled 
} from './webSocketService.js'

// 工具函数
export const serviceUtils = {
  /**
   * 格式化时间戳
   */
  formatTimestamp(timestamp) {
    if (!timestamp) return ''
    const date = new Date(timestamp)
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
  },

  /**
   * 计算时间差
   */
  formatDuration(startTime, endTime) {
    if (!startTime || !endTime) return ''
    
    const start = new Date(startTime)
    const end = new Date(endTime)
    const diffMs = end - start
    
    if (diffMs < 1000) {
      return `${diffMs}ms`
    } else if (diffMs < 60000) {
      return `${(diffMs / 1000).toFixed(1)}s`
    } else {
      const minutes = Math.floor(diffMs / 60000)
      const seconds = Math.floor((diffMs % 60000) / 1000)
      return `${minutes}m ${seconds}s`
    }
  },

  /**
   * 格式化文件大小
   */
  formatFileSize(bytes) {
    if (bytes === 0) return '0 Bytes'
    
    const k = 1024
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))
    
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
  },

  /**
   * 生成唯一ID
   */
  generateId() {
    return Date.now().toString(36) + Math.random().toString(36).substr(2)
  },

  /**
   * 深度克隆对象
   */
  deepClone(obj) {
    return JSON.parse(JSON.stringify(obj))
  },

  /**
   * 防抖函数
   */
  debounce(func, wait) {
    let timeout
    return function executedFunction(...args) {
      const later = () => {
        clearTimeout(timeout)
        func(...args)
      }
      clearTimeout(timeout)
      timeout = setTimeout(later, wait)
    }
  },

  /**
   * 节流函数
   */
  throttle(func, limit) {
    let inThrottle
    return function executedFunction(...args) {
      if (!inThrottle) {
        func.apply(this, args)
        inThrottle = true
        setTimeout(() => inThrottle = false, limit)
      }
    }
  }
}

export default {
  imageUpdateService,
  webSocketService,
  serviceUtils
}